package com.effectiv.gooruda.mvc.controller;

import java.util.List;

import javax.validation.Valid;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.effectiv.gooruda.business.api.ApplicationService;
import com.effectiv.gooruda.business.api.SocialProviderService;
import com.effectiv.gooruda.domain.Application;
import com.effectiv.gooruda.domain.SocialProvider;
import com.effectiv.gooruda.mvc.argumentresolver.AjaxResponse;


@Slf4j
@Controller
public class ApplicationContoller {
		
	@Autowired
	private ApplicationService applicationService;
	@Autowired
	private SocialProviderService socialProviderService;
	
	@RequestMapping(value = "/newapplication")
	public final String newApplication(Model model) {
			
		model.addAttribute("app",BeanUtils.instantiate(Application.class));
		return "newapplication";
	}
	
	@RequestMapping(value = "/editapplication")
	public final String edit(@RequestParam("application") String id, Model model) {
		Application app = applicationService.findOne(id);
		
		List<SocialProvider> providers = app.getSocialProviders();
		
		model.addAttribute("app",applicationService.findOne(id));
		model.addAttribute("providers", providers);
		return "editapplication";
	}
	
	@RequestMapping(value = "/updatekey")
	public final String updateKey(@RequestParam("application") String id,
				@RequestParam("type") String type, Model model) {
		Application app = applicationService.updateKey(id, type);	
		model.addAttribute("app",app);
		model.addAttribute("keyupdated",true);
		return "editapplication";
	}
	
	@RequestMapping(value = "/updateapp", method = RequestMethod.POST)
	public @ResponseBody
	AjaxResponse update(@Valid Application app, BindingResult result,
			AjaxResponse response) {
		
		log.info("Application = {}", app);
		log.info("Application # = {}", app.getId());
		
		if (!result.hasErrors()) {
			applicationService.update(app);
			response.setResult(app);
		} else {

			response.markValidationFailure(result.getFieldErrors());
		}
		log.info("AjaxResponse = {}", response);
		return response;
	}
	

	@RequestMapping(value = "/saveapplication", method = RequestMethod.POST)
	public @ResponseBody
	AjaxResponse save(@Valid Application app, BindingResult result,
			AjaxResponse response) {

		if (!result.hasErrors()) {
			applicationService.save(app);
			response.setResult(app);
		} else {

			response.markValidationFailure(result.getFieldErrors());
		}
		log.info("AjaxResponse = {}", response);
		return response;
	}
	
	
	@RequestMapping(value = "/listapplications")
	public final String listApplications(Model model) {
		model.addAttribute("applications", applicationService.findByTenant());	
		return "listapplications";
	}
}
